package leecode.q977;

public class Solution {
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int[] res = new int[nums.length];
        int last = right;
        while (left != right) {
            int lefts = nums[left] * nums[left];
            int rights = nums[right] * nums[right];
            if (lefts >= rights) {
                res[last--] = lefts;
                left++;
            } else {
                res[last--] = rights;
                right--;
            }
        }
        res[last] = nums[left] * nums[left];
        return res;
    }
}
